<?php
session_start();
require_once '../../includes/global.php';


//得到配置文件
$wcfg=Dga('wcfg',"web like '".$sitename."' and lg like '".$_SESSION[LG]."'");
$wcfg=json_decode($wcfg[wcfg],true);
$dcfg=Dga('dcfg',"id like '".$_POST[did]."'");
$dcfg=json_decode($dcfg[dcfg],true);
//Jc($wcfg);
//检测多语言屏蔽
if(array_key_exists($dcfg['name'],$wcfg['ping'])){
	$wcfg['num']=1;
	$temp=$wcfg['lang'][$wcfg['ping'][$dcfg['name']]];
	unset($wcfg['lang']);
	$wcfg['lang'][$wcfg['ping'][$dcfg['name']]]=$temp;
}


//print_r($_FILES);
		
//Jc('p');
//Jc('f');
//exit;
foreach($wcfg['lang'] as $lkey =>$lg){
	
		foreach($dcfg[structure] as $dkey =>$dvalue){
			switch ($dvalue['ftype']){
				case "phpfile":{
				Fw($_SERVER['DOCUMENT_ROOT'].TMPE_PATH."admin/db/".$lkey.$_POST[$lkey]['gid'].".php",html_entity_decode(stripslashes($_POST[$lkey][$dkey])));
				require_once ( $_SERVER['DOCUMENT_ROOT'].TMPE_PATH."admin/db/".$lkey.$_POST[$lkey]['gid'].".php");
				$_POST[$lkey][$dkey]=json_encode($tjson);
				unset($tjson);
				break;
				}
				case "textarea":{
				$_POST[$lkey][$dkey]=html_entity_decode(stripslashes($_POST[$lkey][$dkey]));
				break;
				}
				case "ubb":{
				$_POST[$lkey][$dkey]=html_entity_decode(stripslashes($_POST[$lkey][$dkey]));
				break;
				}
				case "select":{
				sort($_POST[$lkey][$dkey]);
				$_POST[$lkey][$dkey]=",".implode(',',$_POST[$lkey][$dkey]).",";
				break;
				}
				case "checkbox":{
				sort($_POST[$lkey][$dkey]);
				$_POST[$lkey][$dkey]=",".implode(',',$_POST[$lkey][$dkey]).",";
				break;
				}
				case "fileup":{
						for($filenum=1;$filenum<=$dvalue['filenum'];$filenum++){
						$cp=1;
						//复制文件
						if(!$_FILES[$lkey]['name'][$dkey][$filenum]){
							//Ja($lg.$dvalue['COMMENT']."字段 第".$filenum."个上传域为空 跳过!");
							$cp=0;
						}		
					$otype=substr(strtolower($_FILES[$lkey]['name'][$dkey][$filenum]),-3);
					if(!in_array($otype,$dvalue['format'])){
							//Ja($lg.$dvalue['COMMENT']."字段 第".$filenum."个上传类型不符 跳过!");
							$cp=0;
					}
					if($_FILES[$lkey]['size'][$dkey][$filenum]>$dvalue['filesize']){
							//Ja($lg.$dvalue['COMMENT']."字段 第".$filenum."个上传大小超过".$dvalue['filesize']."字节(bit)跳过!");
							$cp=0;
					}
				//复制文件
				if($cp){
				$filename="../../uploads/pic/".$lkey.$_POST[$lkey]['gid'].$filenum.".".$otype;
				$tempname[$lkey][]=$lkey.$_POST[$lkey]['gid'].$filenum.".".$otype;
				@unlink($filename);
				$copymes=copy($_FILES[$lkey]['tmp_name'][$dkey][$filenum],$filename);
				}
				//缩略图
					if(in_array($otype,$dvalue['thumb'])){
						createthumb($_FILES[$lkey]['tmp_name'][$dkey][$filenum],".".$otype,$lkey.$_POST[$lkey]['gid'].$filenum);
					}
				//图片水印
					if($dvalue['watermarkpic']){
						if(in_array($otype,$dvalue['watermark'])){
						 imageWaterMark($filename,$dvalue['picposition'],$_SERVER['DOCUMENT_ROOT'].TMPE_PATH.$dvalue['watermarkpic'],"",5,"#FFFFFF"); 
						 }
					}
				//文字水印
					if($dvalue['watermarktxt']){
						if(in_array($otype,$dvalue['watermark'])){
						//echo $filename.",".$dvalue['txtposition'].",\"\",".$dvalue['watermarktxt'].",5,\"#FFFFFF\"";
			imageWaterMark($filename,$dvalue['txtposition'],"",$dvalue['watermarktxt'],5,"#FFFFFF");
							 }
					}
				
				       
					}
				$tempic=Dga($dcfg['name']," gid ='".$_POST[$lkey]['gid']."' and lg like '".$lkey."' ");
					$tempic=explode(',',$tempic[$dkey]);
					//Jc($tempic);
					//Jc($tempname);
					foreach($tempic as $picvalue){
							$num=substr($picvalue,-5,1);
							if($tempname[$lkey][$num]==''){
								$tempname[$lkey][$num]=$picvalue;
							}			
					}
				
				$_POST[$lkey][$dkey]=implode(",",$tempname[$lkey]);
				break;
				}
			}
		}

//操作数据库
		if($_POST[$lkey][id]==''){
		unset($_POST[$lkey][id]);
		Di($dcfg[name],$_POST[$lkey]);
		foreach($dcfg[structure] as $dkey =>$dvalue){
			switch ($dvalue['ftype']){
				case "back":{
					backdselect(mysql_insert_id(),$dcfg[name]);
				break;
				}
			}
		}
		}else{
		Du($dcfg[name],$_POST[$lkey],'id',$_POST[$lkey][id]);
		foreach($dcfg[structure] as $dkey =>$dvalue){
			switch ($dvalue['ftype']){
				case "back":{
					backdselect($_POST[$lkey][id],$dcfg[name]);
				break;
				}
			}
		}
		}
}
//echo "OK;";
//exit;
Jt($_SERVER['HTTP_REFERER'],'更新成功');
?>